<?php

namespace Biz\model\trade;

use Biz\model\trade\GroupJoin;
use Medoo\Medoo;

/**
 * 开团用户记录
 */
class GroupJoinMember extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'group_join_member';

    //角色类型
    const ROLE_LEADER = 1;
    const ROLE_PARTNER = 2;


    public static function addData($group_join_id, $group_id, $type, $uid, $shop_id, $goods_id, $sku_id, $role, $order_no, $status = GroupJoin::STATUS_WAIT_PAY, $ext = '')
    {
        $addData = [
            'group_join_id'  => $group_join_id,
            'group_id'  => $group_id,
            'type'      => $type,
            'uid'       => $uid,
            'role'   => $role,
            'shop_id'   => $shop_id,
            'goods_id'  => $goods_id,
            'sku_id'  => $sku_id,
            'order_no'  => $order_no,
            'status' => $status,
            'ext' => $ext,
//            'start_time'=> date('Y-m-d H:i:s'),
        ];
        return self::insert($addData)->rowCount();
    }

    public static function getInfoByOrderNo($order_no)
    {
        $where = [
            'order_no' => $order_no,
        ];
        return self::get('*', $where);
    }

    public static function getAdminInfoByOrderNos($order_no)
    {
        $where = [
            'order_no' => $order_no,
            'status' => [
                GroupJoin::STATUS_ING,
                GroupJoin::STATUS_SUCCESS,
                GroupJoin::STATUS_FAIL,
            ]
        ];
        return self::select('*', $where);
    }

    public static function getGroupJoinCount($uid, $group_join_id = 0)
    {
        $where = [
            'uid'       => $uid,
            'group_join_id'    => $group_join_id,
            'status[!]' => [
                GroupJoin::STATUS_DEL
            ]
        ];
        return self::count($where);
    }

    public static function getJoinCount($uid, $group_id = 0)
    {
        $where = [
            'uid'       => $uid,
            'role'      => self::ROLE_PARTNER,
            'status'    => [
                GroupJoin::STATUS_ING
            ],
        ];
        if ($group_id > 0) {
            $where['group_id'] = $group_id;
        }
        return self::count($where);
    }

    public static function getJoinMember($group_join_id, $appendStatus = [])
    {
        $status = [
            GroupJoin::STATUS_ING
        ];
        $status = array_merge($status, $appendStatus);
        $where = [
            'group_join_id' => $group_join_id,
            'status'        => $status,
        ];
        return self::select_master('*', $where);
    }

    public static function getJoinStock($group_join_id)
    {
        $where = [
            'AND' => [
                'group_join_id' => $group_join_id,
                "status"        => GroupJoin::STATUS_ING
            ],
            'GROUP' => [
                'sku_id'
            ]
        ];
        return GroupJoinMember::select([
            'sku_id',
            'stock' => Medoo::raw('COUNT(sku_id)')
        ], $where);
    }
}

